<?php
/*
 * 商家的数据库操作类
 * @package    Db
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: d4a1d976dc5a5f4b1ba102522dd65e4a5b8a579f $
 */
defined('IN_IXDCW') or exit('Access Denied');
final class BusinessDb extends CoreDb{

	public function __construct(){
		parent::__construct();
		$this->_businessAuthDb = Db('businessAuth');
		$this->_businessAuthTable = $this->_businessAuthDb->tableName();
	}

	public function getByItemid($itemid) {
		$sql = "
			SELECT b.*
			FROM {$this->tableName()} b
			WHERE b.itemid={$itemid}";
		return $this->_db->getOne($sql);
	}

	/**
	 * 获取会员数量
	 * @param string $condition
	 * @return int
	 */
	public function getCount($condition = '') {
		$sqlMatchedNum = "
			SELECT COUNT(b.itemid) num
			FROM {$this->tableName()} b, {$this->_businessAuthTable} a
			WHERE b.userid =a.userid";
		if ($condition) $sqlMatchedNum .= $condition;
		return intval($this->_db->getVar($sqlMatchedNum, 'num'));
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @return array
	 */
	public function getList($condition = ''){
		$memberTable = DbMember::$table_name;
		$sqlList = "
			SELECT b.*, m.username
			FROM {$this->tableName()} b, {$this->_businessAuthTable} a, {$memberTable} m
			WHERE b.userid =a.userid and m.userid=b.userid";
		if ($condition) $sqlList .= $condition;
		$lists = $this->_db->getAll($sqlList);
		return $lists;

	}

	/**
	 * 更新license 状态
	 * @param $itemid
	 * @param $status
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function updateBusinessStatus($itemid, $status) {
		$sql = "update " . $this->tableName() . " set status=".$status .' where itemid='.$itemid;
		return $this->_db->query($sql);
	}
}